package cn.tingting.controller;

import cn.tingting.dto.CourseDto;
import cn.tingting.esdoc.CourseDoc;
import cn.tingting.query.CourseDocQuery;
import cn.tingting.service.ICourseService;
import cn.tingting.domain.Course;
import cn.tingting.query.CourseQuery;
import cn.tingting.util.AjaxResult;
import cn.tingting.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;


    /**
     * 保存和修改公用的
     */
    @PutMapping("/addorupdae")
    @PreAuthorize("hasAnyAuthority('course:addorupdae')")
    public AjaxResult addOrUpdate(@RequestBody CourseDto courseDto){
        try {
            if( courseDto.getCourse().getId()!=null){
                System.out.println("修改");
                courseService.updateByIdplus(courseDto);
            }
            else {
                System.out.println("添加");
                courseService.insertplus(courseDto);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize("hasAnyAuthority('course:del')")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            courseService.deleteByIdplus(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('course:list')")
    public List<Course> list(){

        return courseService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")

    public PageList<Course> json(@RequestBody CourseQuery query)
    {
//        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
//        page = courseService.selectPage(page);
        return courseService.ListCourse(query);

    }
    //上线
    @PostMapping("/onLineCourse/{id}")
    @PreAuthorize("hasAuthority('course:onLineCourse')")
    public AjaxResult onLineCourse(@PathVariable("id")Long id){
      return courseService.onLineCourse(id,true);
    }

    @PostMapping("/offLineCourse/{id}")
    @PreAuthorize("hasAuthority('course:offLineCourse')")
    public AjaxResult offLineCourse(@PathVariable("id")Long id){
       return courseService.onLineCourse(id,false);

    }


    @PostMapping("/queryCourses")
    public AjaxResult queryCourses(@RequestBody CourseDocQuery query){
        PageList<CourseDoc> courseDocPageList = courseService.queryCourses(query);
        return  AjaxResult.me().setResultObj(courseDocPageList);

    }
}
